package com.j2eefast.produce.pms.controller;

import com.j2eefast.common.core.base.entity.Ztree;
import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.utils.ResponseData;
import com.j2eefast.common.core.utils.ValidatorUtil;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.framework.sys.service.SysUserService;
import com.j2eefast.framework.utils.UserUtils;
import com.j2eefast.produce.pms.entity.PmsTemplateTypeEntity;
import com.j2eefast.produce.pms.entity.PmsTestProjectClassifyEntity;
import com.j2eefast.produce.pms.service.PmsTemplateTypeService;
import com.j2eefast.produce.pms.service.PmsTestProjectClassifyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>检测项目分类</p>
 *
 * @author: summer
 * @date: 2021-05-29 22:03
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Controller
@RequestMapping("/pms/templateType")
public class PmsTemplateTypeController extends BaseController {
	private String urlPrefix = "modules/pms/templateType";
	@Autowired
	private PmsTemplateTypeService pmsTemplateTypeService;


	@RequiresPermissions("pms:templateType:view")
	@GetMapping()
	public String process() {
		return urlPrefix + "/index";
	}

	/**
	 * 页面获取公司信息列表
	 * @param params
	 * @return
	 */
	@RequestMapping("/list")
	@RequiresPermissions("pms:templateType:list")
	@ResponseBody
	public List<PmsTemplateTypeEntity> list(@RequestParam Map<String, Object> params) {
		return  pmsTemplateTypeService.findList(params);
	}

	/**
	 * 加载公司列表树
	 */
	@GetMapping("/treeData")
	@RequiresPermissions("pms:templateType:list")
	@ResponseBody
	public List<Ztree> treeData(Long parentId){
		List<Ztree> ztrees = pmsTemplateTypeService.findCompTree(parentId);
		return ztrees;
	}

	/**
	 * 新增设备分类
	 */
	@GetMapping("/add/{id}")
	public String add(@PathVariable("id") Long id, ModelMap mmap){
		//mmap.put("comp",  busClassifyService.findCompById(id));
		PmsTemplateTypeEntity pmsTemplateTypeEntity = pmsTemplateTypeService.findCompById(id);
		mmap.put("templateType",  pmsTemplateTypeEntity);
		return urlPrefix + "/add";
	}

//	/**
//	 * 新增参数配置
//	 */
//	@RequiresPermissions("pms:templateType:add")
//	@GetMapping("/add")
//	public String add() {
//		return urlPrefix + "/add";
//	}
	/**
	 * 保存配置
	 */
	@BussinessLog(title = "检验列表", businessType = BusinessType.INSERT)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@RequiresPermissions("pms:templateType:add")
	@ResponseBody
	public ResponseData save(@Validated PmsTemplateTypeEntity comp) {
//		busClassifyService.checkDataScope(comp.getParentId());
		ValidatorUtil.validateEntity(comp);
//		if (!busClassifyService.checkCompNameUnique(comp)){
//			return error("设备分类'" + comp.getClassName() + "'失败,名称已存在");
//		}
		pmsTemplateTypeService.add(comp);
		return success();
	}

	/**
	 * 新增设备分类
	 */

	/**
	 * 新增参数配置
	 */
	@RequiresPermissions("pms:templateType:addfrom")
	@GetMapping("/addfrom")
	public String addfrom() {
		System.out.println("新增参数配置");
		return urlPrefix + "/addfrom";
	}

	/**
	 * 修改出入库 页面
	 */
	@RequiresPermissions("bus:templateType:edit")
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		System.out.println(id);
		PmsTemplateTypeEntity pmsTemplateTypeEntity = pmsTemplateTypeService.findCompById(id);
		System.out.println(pmsTemplateTypeEntity.getName());
		mmap.put("templateType", pmsTemplateTypeEntity);

		return urlPrefix + "/edit";
	}

	/**
	 * 修改出入库保存
	 */
	@RepeatSubmit
	@RequiresPermissions("bus:templateType:edit")
	@BussinessLog(title = "修改", businessType = BusinessType.UPDATE)
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData edit(PmsTemplateTypeEntity pmsTemplateTypeEntity) {
		ValidatorUtil.validateEntity(pmsTemplateTypeEntity);
		Date date = new Date();//创建一个时间对象，获取到当前的时间
		pmsTemplateTypeEntity.setUpdateTime(date);
//		Long userId= UserUtils.getUserInfo().getId();
//		SysUserEntity sysUserEntity=busEquipmentService.getSysById(userId);
//		busEquipmentEntity.setUpdateBy(sysUserEntity.getName());
		String userName= UserUtils.getUserInfo().getName();
		pmsTemplateTypeEntity.setCreateBy(userName);
		System.out.println("修改出入库保存");
		System.out.println("修改出入库保存");
		System.out.println("修改出入库保存");
		System.out.println("修改出入库保存");
		return pmsTemplateTypeService.updateById(pmsTemplateTypeEntity) ? success() : error("修改失败!");
	}


	/**
	 * 删除检验
	 */
	/**
	 * 合同删除
	 */
	@RepeatSubmit
	@RequiresPermissions("pms:templateType:del")
	@BussinessLog(title = "删除", businessType = BusinessType.UPDATE)
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData doDelete(HttpServletRequest request) {
		Long  ids=Long.parseLong(request.getParameter("id")) ;
		return pmsTemplateTypeService.delSysCompById(ids) ? success() : error("修改失败!");
	}

}
